<template>
	<section>
		<!--工具条(查询条件)-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-select v-model="filters.cardState" placeholder="卡状态">
						<el-option
						  v-for="item in options_cardstate"
						  :label="item.label"
						  :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.cardType" placeholder="套餐">
						<el-option
						  v-for="item in options_cardtype"
						  :label="item.label"
						  :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.inSkuBt"
					  type="date"
					  placeholder="入库开始日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.inSkuEt"
					  type="date"
					  placeholder="入库截止日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.cardSegBt" placeholder="卡号段开始"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.cardSegEt" placeholder="卡号段截止"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.likeCardKey" placeholder="输卡号/ICCID/IMSI/查询码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getCards">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">批量入库</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="showBatchDel">批量删除</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="exportExcel">导出EXCEL</el-button>
				</el-form-item>				
			</el-form>
		</el-col>

		<!--列表(查询结果-当前页)-->
		<el-table :data="cards" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<!--
			<el-table-column type="selection" width="55">
			</el-table-column>
			-->
			<el-table-column prop="msisdn" label="卡号" width="180" sortable>
			</el-table-column>
			<el-table-column prop="iccid" label="ICCID" width="180" sortable>
			</el-table-column>
			<el-table-column prop="imsi" label="IMSI" width="180" sortable>
			</el-table-column>
			<el-table-column prop="querycode" label="查询代码" width="120" sortable>
			</el-table-column>
			<el-table-column prop="packagename" label="套餐名称" min-width="180" sortable>
			</el-table-column>
			<el-table-column prop="apicode" label="API代码" min-width="100" sortable>
			</el-table-column>
			<el-table-column prop="addtime" label="入库日期" min-width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleMore(scope.$index, scope.row)">详细</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条(分页)-->
		<el-col :span="24" class="toolbar">
			<!--
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			-->
			<el-pagination
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
			:current-page="currentPage"
			:page-sizes="[10, 20, 50, 100]"
			:page-size="pageSize"
			layout="total, sizes, prev, pager, next, jumper"
			:total="total"
			style="float:right;">
			</el-pagination>
		</el-col>

		<!--批量入库界面-->
		<el-dialog title="批量入库" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="0px" :rules="addFormRules" ref="addForm">
				<el-form-item label="">
					<el-upload
					  ref="upInSkuFile"
					  class="upload-demo"
					  :action="action"
					  :on-preview="handlePreview"
					  :on-remove="handleRemove"
					  :file-list="addForm.fileList"
  					  :auto-upload="false"
					  :show-file-list="false"
					  :before-upload="beforeUploadValid"	
					  :on-change="handleChange"
					  :on-progress="handleProgress"
					  :on-success="HandleSuccess"
					>
					  <el-button size="small" type="primary" icon="upload">上传模板文件</el-button>
					  <div slot="tip" class="el-upload__tip">
						<el-col :span="12">
					  		支持扩展名.xsl,.xslx,且不超过50M
						</el-col>
						<el-col :span="12">
							文件:TODO:目录文件地址
						</el-col>					
					  </div>
					</el-upload>
				</el-form-item>
				<el-form-item label="" v-show="addForm.stepsIsShow">
					<el-steps  :active="addForm.stepNo" :center="true">
					  <el-step title="检测模板文件" icon="circle-check"></el-step>
					  <el-step title="检测数据一致性" icon="circle-check"></el-step>
					  <el-step title="正在导入" icon="circle-check"></el-step>
					  <el-step title="导入完成" icon="circle-check"></el-step>
					</el-steps>
				</el-form-item>
				<el-form-item label="" v-show="addForm.progressIsShow">
					<el-progress :text-inside="true" :stroke-width="12" :percentage="addForm.progressNo"></el-progress>
				</el-form-item>
				<el-form-item label=""  v-show="addForm.errtextIsShow">
					<el-input
					  type="textarea"
					  :rows="4"
					  :readonly="true"
					  resize="none"
					  placeholder=""
					  v-model="addForm.errtextarea">
					</el-input>
				</el-form-item>
				<el-form-item label="">
					<el-col :span="12">
						<el-checkbox v-model="addForm.imp_syn_checked">导入时同步API</el-checkbox>
					</el-col>
					<el-col class="line" :span="2" style="text-align:center;"></el-col>
					<el-col :span="12" style="text-align:right;">
						<el-form-item label="">入库日期：<el-date-picker type="date" placeholder="选择日期" v-model="addForm.inSkuTime" :picker-options="pickerOptions0"></el-date-picker></el-form-item>
					</el-col>
				</el-form-item>
			
			</el-form>
			<div slot="footer" class="dialog-footer" >
				<el-col :span="12" style="text-align:left;margin-bottom:15px;">
					<el-button icon="arrow-down" @click.native="downImpExcel">下载模板</el-button>
				</el-col>
				<el-col :span="12">
					<el-button @click.native="addFormVisible = false">取消</el-button>
					<el-button type="primary" @click.native="addSubmit" :loading="addLoading">导入</el-button>
				</el-col>						
			</div>
		</el-dialog>
		
		<!--批量删除界面-->
		<el-dialog title="批量删除" v-model="delFormVisible" :close-on-click-modal="false">
			<el-form :model="delForm" ref="delForm" :rules="delFormRules" :inline="true" >
				<el-form-item label="卡号段：" prop="cardFromNo">
					<el-input v-model="delForm.cardFromNo" placeholder="请输入MSISDN"></el-input>
				</el-form-item>
				<el-form-item label="至"  prop="cardToNo">
					<el-input v-model="delForm.cardToNo" placeholder="请输入MSISDN"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="delFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="batchRemove" :loading="delLoading">删除</el-button>
			</div>
		</el-dialog>
		
		<!--卡号信息详情界面-->
		<cardinfo ref="cardinfo"></cardinfo>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	import constant from '../../common/js/constant'
	//import NProgress from 'nprogress'
	import { getCardListPage, removeCard, batchRemoveUser, editUser, addUser } from '../../api/api';
	import Cardinfo from '../../components/card_info.vue';

	export default {
		components: {
			cardinfo: Cardinfo
  		},
		data() {
			return {
				pickerOptions0: {
					disabledDate(time) {
						return time.getTime() < Date.now() - 8.64e7;
					}
				},
				options_cardstate: constant.opt_cardstate,
				options_cardtype: constant.opt_cardtype,
				filters: {
					//卡状态
					cardState: '',
					//卡套餐名称类型
					cardType: '',
					//入库开始时间
					inSkuBt: '',
					//入库截止时间
					inSkuEt: '',
					//卡号段开始
					cardSegBt: '',
				    //卡号段截止				
					cardSegEt: '',
					//模糊查询卡关键字
					likeCardKey: ''
				},
				cards: [],
				total: 0,//列表总条数
				pageSize:10,//每页条数
				currentPage:1,//当前页码
				listLoading: false,
				sels: [],//列表选中列

				action:"/a",
				addFormVisible: false,//批量入库界面是否显示
				addLoading: false,
				addFormRules: {
					inSkuTime: [
						{ required: true, message: '请选择入库时间', trigger: 'blur' }
					]
				},
				//批量入库界面数据
				addForm: {
					fileList:[],
					stepsIsShow:true,
					stepNo:0,
					progressIsShow:false,
					progressNo:0,
					errtextIsShow:false,
					errtextarea:'',
					imp_syn_checked:true,
					inSkuTime: new Date()
				},

				delFormVisible: false,//批量删除界面是否显示
				delLoading: false,
				delFormRules: {
					cardFromNo: [
						{ required: true, message: '请输入MSISDN开始号段', trigger: 'blur' }
					],
					cardToNo: [
						{ required: true, message: '请输入MSISDN截至号段', trigger: 'blur' }
					]					
				},
				delForm: {
					cardFromNo: '',
					cardToNo: ''
				}
				

			}
		},
		methods: {

			handlePreview(file){
				//console.log(file);
			},
			handleChange(file, fileList){
				//this.addForm.fileList = fileList.slice(-1);
			},
			handleProgress(file, fileList){
				alert("正在处理中");
			},
			HandleSuccess(){
				alert("成功");
			},
			handleRemove(){
			},
			beforeUploadValid(file) {

				this.addForm.stepsIsShow=true;
				this.addForm.stepNo=1;
				this.addForm.progressIsShow=true;
				this.addForm.progressNo=20;

				var fileType=file.name.substring(file.name.lastIndexOf("."),file.name.length)  
    			fileType=fileType.toLowerCase();
				//文件格式
				const isExcel = (fileType === '.xls') || (fileType === '.xlsx');
				//文件大小
				const isLt2M = file.size / 1024 / 1024 < 50;

				if (!isExcel) {
					//this.$message.error('上传文件扩展名只能是 xls/xlsx 格式!');
					this.addForm.errtextIsShow=true;
					this.addForm.errtextarea='上传文件扩展名只能是 xls/xlsx 格式!';
					return isExcel && isLt2M;
				}
				if (!isLt2M) {
					//this.$message.error('上传文件大小不能超过 50MB!');
					this.addForm.errtextIsShow=true;
					this.addForm.errtextarea='上传文件扩展名只能是 xls/xlsx 格式!';
					return isExcel && isLt2M;
				}
				//console.log(file);
				this.addForm.stepNo=2;
				this.addForm.progressNo=40;
				alert(11);
				return isExcel && isLt2M;
      		},
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			exportExcel(){
				//location.href="/api/user/exportXls?pageSize="+this.pageSize+"&pageNum="+this.currentPage+"&userid="+123;
				location.href="/api/user/exportXls?userid="+123;
			},
			downImpExcel(){
				location.href="/导入模板.xlsx";
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getCards();
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getCards();
			},
			
			//获取用户列表
			getCards() {
				let para = {
					pageSize:this.pageSize,
					pageNum:this.currentPage			
				};
				para = Object.assign(para, this.filters);

				this.listLoading = true;
				//NProgress.start();
				getCardListPage(para).then((res) => {
					this.total = res.data.total;
					this.cards = res.data.list;
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.msisdn };
					removeCard(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getCards();
					});
				}).catch(() => {

				});
			},
			//显示卡号详细界面
			handleMore: function (index, row) {
				 this.$refs.cardinfo.showCardDetail(row.msisdn);
			},
			//显示批量入库界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					fileList:[],
					stepsIsShow:true,
					stepNo:0,
					progressIsShow:false,
					progressNo:0,
					errtextIsShow:false,
					errtextarea:'',
					imp_syn_checked:true,
					inSkuTime: new Date()
				};
			},
			//显示批量删除界面
			showBatchDel: function () {
				this.delFormVisible = true;
				this.delForm = {
					cardFromNo: '',
					cardToNo: ''
				};
			},
			//新增
			addSubmit: function () {
				this.addForm.inSkuTime = (!this.addForm.inSkuTime || this.addForm.inSkuTime == '') ? '' : util.formatDate.format(new Date(this.addForm.inSkuTime), 'yyyy-MM-dd');
				//alert(this.addForm.inSkuTime);
				this.action="/a?inSkuTime="+this.addForm.inSkuTime;
				this.addForm.stepsIsShow=true;
				this.addForm.progressIsShow=true;
				this.addForm.errtextIsShow=true;
         		this.$refs.upInSkuFile.submit();

			    /*
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							addUser(para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getCards();
							});
						});
					}
				});
				*/

			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {

				
				this.$refs.delForm.validate((valid) => {
					if (valid) {
						
						var ids = this.sels.map(item => item.id).toString();
						this.$confirm('确认删除选中记录吗？', '提示', {
							type: 'warning'
						}).then(() => {
							this.listLoading = true;
							//NProgress.start();
							let para = { ids: ids };
							batchRemoveUser(para).then((res) => {
								this.listLoading = false;
								//NProgress.done();
								this.$message({
									message: '删除成功',
									type: 'success'
								});
								this.getCards();
							});
						}).catch(() => {

						});


					}
				});





			}
		},
		mounted() {
			this.getCards();
		}
	}

</script>

<style scoped>

</style>